{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cd6f4996",
   "metadata": {},
   "source": [
    "## 列表\n",
    "* 通过[]来创建一个list结构\n",
    "* 里面放任何类型都可以的，没有一个长度限制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e6d9cdc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang=[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1c8247a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(tang)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7512e9b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=[1,2,3,4]\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0991ff01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', '3', '4']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=[\"1\",\"2\",\"3\",\"4\"]\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "86895ae6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 'tangyudi', 3, 5]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=[1,'tangyudi',3,5]\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "23eb657a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang =list()\n",
    "type(tang)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a7800f7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang =list([1,2,3])\n",
    "tang"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48654934",
   "metadata": {},
   "source": [
    "## list操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0f037ecb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(tang)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c91e6159",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[123, 456, 'tang', 'yudi']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a =[123,456]\n",
    "b = [\"tang\",\"yudi\"]\n",
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8ec57f02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4d1b34ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[123, 456, 123, 456]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0cc811aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[123, 456, 123, 456, 123, 456]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "459af836",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "123"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5e17b979",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[123, 456]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "797d38db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "456"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4541ae2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[789, 456]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0]=789\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d2f06dfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tang', 'yudi']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:]=[\"tang\",\"yudi\"]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f0568918",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 7]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[1,2,3,4,5,6,7,8,9]\n",
    "a[4:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "07e8ea9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f2963d27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del a[0]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9588d676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del a[3:]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "11db537e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=[1,2,3,4,5,6,7,8,9]\n",
    "8  in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "592e90cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang = \"tang yu di\"\n",
    "'tang'in tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "3b87680c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, [3, 4]]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a =[1,2,[3,4]]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7f5020ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 4]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "708d960d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b2c34974",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=['apple','banna','apple','banna','apple','banna']\n",
    "tang.count(\"apple\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "866946f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=['apple','1','2','3','4','5','6']\n",
    "tang.index('2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1b7bfaf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'apple'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5843ce0c",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "'bannaa' is not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[42], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m tang\u001b[38;5;241m.\u001b[39mindex(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbannaa\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mValueError\u001b[0m: 'bannaa' is not in list"
     ]
    }
   ],
   "source": [
    "tang.index(\"bannaa\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab660b92",
   "metadata": {},
   "source": [
    "## 列表添加\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "30262a09",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang=[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "24119303",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tang', 'tang', 'tang']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.append(\"tang\")\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "d915980d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tang', 'tang', 'tang', ['tang', 'yudi']]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.append([\"tang\",\"yudi\"])\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "fc522767",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tang', 'tang', 'python', 'tang', ['tang', 'yudi']]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.insert(2,'python')\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "780a0b03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tang', 'tang', 'python', 'tang']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.remove(['tang', 'yudi'])\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "7126d8d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python', 'tang']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.remove(\"tang\")\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "38f84693",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tang'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "ac36c219",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['python']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "8e539981",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=[3,5,1,7,2,8,6,9]\n",
    "tang.sort()\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "1690bf56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 5, 1, 7, 2, 8, 6, 9]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=[3,5,1,7,2,8,6,9]\n",
    "tang2=sorted(tang)\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "746a6de4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "eef23411",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tang', 'yu', 'di']"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=['di','yu','tang']\n",
    "tang.reverse()\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f448e8f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
